from pycparser import c_ast, c_generator
import sys
sys.path.extend(['.', '..'])
from c_ast_decl import *
builder = ASTBuilder()

# 定义函数
with builder.function_def("main", [], "int"):
    # 声明变量 i（注册到 main 函数作用域）
    i_decl = builder.create_variable_decl('i', 'int', init='3')
    
    # 嵌套循环
    with builder.for_loop(
        init=i_decl,
        cond=builder.create_binary_op(op='<', left='i', right='5'),
        step=builder.create_unary_op(op='++', expr='i')
    ):
        # 在循环作用域内声明变量 j
        j_decl = builder.create_variable_decl('j', 'int', init='0')
        
        # 检查符号表
        current_scope = builder.scope_stack[-1]
        # print(current_scope.lookup_variable('i'))  # 输出: int (继承自外层作用域)
        # print(current_scope.lookup_variable('j'))  # 输出: int
        
        # 添加 break（正确关联到 for 循环）
        builder.add_break()
        


print(builder.generate_source())